﻿using System;
using System.Collections.Generic;
using System.Data;
using CoreLab.Oracle;
using LMD.Lib.Oracle;
using LMD.Lib.DataClass;
using System.Linq;

namespace Controller
{
    /// <summary>
    /// Summary description for marketing activity attendant Controller
    /// </summary>
    
    public class NPSMarketingActivityAttendantController : ControllerBase
    {

        public void validateNPSMarketingActivityAttendants(Entities.NPSMarketingActivity ciMarketingActivity)
        {
            List<Entities.NPSMarketingActivityAttendant> StoredAttendantList= getNPSMarketingActivityAttendantList(ciMarketingActivity);
            foreach (Entities.NPSMarketingActivityAttendant Attendant in ciMarketingActivity.NPSMarketingActivityAttendantList)
            {
                if (StoredAttendantList.Where(x => x.iContactPersonID.Equals(Attendant.iContactPersonID)).Count() == 0)
                {
                    InsertNPSMarketingActivityAttendantData(Attendant);
                }
            }
            foreach (Entities.NPSMarketingActivityAttendant Attendant in StoredAttendantList)
            {
                if (ciMarketingActivity.NPSMarketingActivityAttendantList.Where(x => x.iContactPersonID.Equals(Attendant.iContactPersonID)).Count() == 0)
                {
                    DeleteNPSMarketingActivityAttendantData(Attendant);
                }
            }
           
        }

        public int InsertNPSMarketingActivityAttendantData(Entities.NPSMarketingActivityAttendant ciMarketingActivityAttendant)
        {
            DBLib dbl = new DBLib();
            List<OracleParameter> lsParams = new List<OracleParameter>();


            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_MARKETING_ACTIVITY_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = ciMarketingActivityAttendant.iNPSMarketingActivityID;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CONTACT_PERSON_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = ciMarketingActivityAttendant.iContactPersonID;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CREATOR";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            String sxQuery = "PR_I_T_NPS_MA_ATTENDANT";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
            return Convert.ToInt32(lsParams[0].Value);
        }

        public void DeleteNPSMarketingActivityAttendantData(Entities.NPSMarketingActivityAttendant ciMarketingActivityAttendant)
        {
            DBLib dbl = new DBLib();
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_M_ACTIVITY_ATTENDANT_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = ciMarketingActivityAttendant.iID;

            String sxQuery = "PR_D_T_NPS_MA_ATTENDANT";
            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
        }

        public List<Entities.NPSMarketingActivityAttendant> getNPSMarketingActivityAttendantList(int MarketingActivityID)
        { 
            Entities.NPSMarketingActivity MarketingActivity=new Entities.NPSMarketingActivity(){iID=MarketingActivityID};
            return getNPSMarketingActivityAttendantList(MarketingActivity);
        }

        public List<Entities.NPSMarketingActivityAttendant> getNPSMarketingActivityAttendantList(Entities.NPSMarketingActivity ciMarketingActivity)
        {
            DBLib dbl = new DBLib();
            String sxQuery = @"
                select 
                                    NPS_M_ACTIVITY_ATTENDANT_ID,
	                                NPS_MARKETING_ACTIVITY_ID,
	                                P_CONTACT_PERSON_ID,
	                                CREATOR_BY,
	                                CREATE_DATE
                                from 
                                    T_NPS_MARKETING_ACTIVITY_ATTENDANT
                                where 
                NVL (VALID_FROM, SYSDATE + 1) < SYSDATE
                        AND NVL (VALID_TO, SYSDATE + 1) > SYSDATE and
                                    NPS_MARKETING_ACTIVITY_ID = :prmID
                ";
            List<OracleParameter> lsParams = new List<OracleParameter>();
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = ciMarketingActivity.iID;
            DataTable dt = dbl.GetDataTable(sxQuery, lsParams.ToArray());

            List<Entities.NPSMarketingActivityAttendant> MarketingActivityAttendantList = new List<Entities.NPSMarketingActivityAttendant>();
            foreach (DataRow row in dt.Rows)
            {
                Entities.NPSMarketingActivityAttendant MarketingActivityAttendant = new Entities.NPSMarketingActivityAttendant();
                MarketingActivityAttendant.iID = Convert.ToInt16(row["T_NPS_M_ACTIVITY_ATTENDANT_ID"].ToString());
                MarketingActivityAttendant.iNPSMarketingActivityID = Convert.ToInt16(row["NPS_MARKETING_ACTIVITY_ID"].ToString());
                MarketingActivityAttendant.iContactPersonID = Convert.ToInt16(row["P_CONTACT_PERSON_ID"].ToString());
                MarketingActivityAttendant.sxCreator = row["CREATE_BY"].ToString();
                MarketingActivityAttendant.dtmCreated = DateTime.Parse(row["CREATE_DATE"].ToString());
                MarketingActivityAttendantList.Add(MarketingActivityAttendant);
            }
            return MarketingActivityAttendantList;
        }

        public Entities.NPSMarketingActivityAttendant getNPSMarketingActivityAttendant(int MarketingActivityAttendantID)
        {
            DBLib dbl = new DBLib();
            String sxQuery = @"
                select 
                            T_NPS_M_ACTIVITY_ATTENDANT_ID,
	                        NPS_MARKETING_ACTIVITY_ID,
	                        P_CONTACT_PERSON_ID,
	                        CREATE_BY,
	                        CREATE_DATE
                        from 
                            T_NPS_MARKETING_ACTIVITY_ATTENDANT
                        where NVL (VALID_FROM, SYSDATE + 1) < SYSDATE
                AND NVL (VALID_TO, SYSDATE + 1) > SYSDATE and
                            T_NPS_M_ACTIVITY_ATTENDANT_ID = :prmID
                ";
            List<OracleParameter> lsParams = new List<OracleParameter>();
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = MarketingActivityAttendantID;
            DataTable dt = dbl.GetDataTable(sxQuery, lsParams.ToArray());

            Entities.NPSMarketingActivityAttendant MarketingActivityAttendant = new Entities.NPSMarketingActivityAttendant();
            try
            {
                MarketingActivityAttendant.iID = Convert.ToInt16(dt.Rows[0]["T_NPS_M_ACTIVITY_ATTENDANT_ID"].ToString());
                MarketingActivityAttendant.iNPSMarketingActivityID = Convert.ToInt16(dt.Rows[0]["NPS_MARKETING_ACTIVITY_ID"].ToString());
                MarketingActivityAttendant.iContactPersonID = Convert.ToInt16(dt.Rows[0]["P_CONTACT_PERSON_ID"].ToString());
                MarketingActivityAttendant.sxCreator = dt.Rows[0]["CREATE_BY"].ToString();
                MarketingActivityAttendant.dtmCreated = DateTime.Parse(dt.Rows[0]["CREATE_DATE"].ToString());

            }
            catch (Exception p) { errLog = p.ToString(); }
            return MarketingActivityAttendant;
        }
    }
}